package com.ys.arch.splitamount.algorithm.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.function.Predicate;

/**
 * <p>@description:</>
 *
 * @author shuang.yang
 * @version 1.0
 * @create 2021/12/25/17:51
 */
public class AmountUtil {

    public static final Integer scale = 4;


    public static Predicate<BigDecimal> predicateBigDecimalNotNullAndNotZero = bigDecimal -> Objects.nonNull(bigDecimal) && bigDecimal.compareTo(BigDecimal.ZERO) != 0;

    public static BigDecimal sub(BigDecimal one, BigDecimal two) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.subtract(two).setScale(scale, RoundingMode.HALF_EVEN);

    }


    public static BigDecimal sub(BigDecimal one, BigDecimal two, int scale) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.subtract(two).setScale(scale, RoundingMode.HALF_EVEN);

    }

    public static BigDecimal abs(BigDecimal amount) {
        return Objects.isNull(amount) ? BigDecimal.ZERO : amount.abs();

    }

    /**
     * 是否相等
     *
     * @param one
     * @param other
     * @param null2Zero
     * @return
     */
    public static boolean compareIsEqual(BigDecimal one, BigDecimal other, boolean null2Zero) {
        if (Objects.isNull(one) && Objects.isNull(other)) {
            return true;
        }
        if (!null2Zero) {
            if (Objects.isNull(one) || Objects.isNull(other)) {
                return false;
            }
        }
        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(other)) {
            other = BigDecimal.ZERO;

        }
        return one.compareTo(other) == 0;


    }

    public static boolean compareNotEqual(BigDecimal one, BigDecimal other, boolean null2Zero) {
        return !compareIsEqual(one, other, null2Zero);
    }

    public static BigDecimal multiply(BigDecimal one, BigDecimal two) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.multiply(two).setScale(scale, RoundingMode.HALF_EVEN);

    }

    public static BigDecimal multiply(BigDecimal one, BigDecimal two, Integer scale, RoundingMode roundingMode) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.multiply(two).setScale(scale, roundingMode);

    }

    public static BigDecimal divide(BigDecimal one, BigDecimal two, int scale) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.divide(two, scale, RoundingMode.HALF_EVEN);

    }
    public static BigDecimal add(BigDecimal one, BigDecimal two, int scale) {

        if (Objects.isNull(one)) {
            one = BigDecimal.ZERO;
        }
        if (Objects.isNull(two)) {
            two = BigDecimal.ZERO;
        }
        return one.add(two).setScale(scale, RoundingMode.HALF_EVEN);
    }

}
